package inet.db.tennis;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import inet.db.pool.*;
import inet.util.Logger;

public class TennisChampionPlayerDAO {

	private Logger logger = null;
	private DBPoolX poolX = null;
	public TennisChampionPlayerDAO(){
		logger = new Logger(this.getClass().getName());
		try{
			poolX = DBPoolX.getInstance(DBPoolXName.SERVICE_BONGDA);
		}catch(Exception exception){
			logger.log(exception.toString()); 
		}
	}
	
	/**
	 * function get a TennisChampionPlayer by id
	 * @param id
	 * @return TennisChampionPlayer
	 */
    public TennisChampionPlayer getRow(BigDecimal id) {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        String strSQL = null;
        TennisChampionPlayer championPlayer = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                " select a.ID, a.CHAMPION_CODE, SEASON_ID, PLAYER_ID, PHASE_ID, SEED, a.LAST_UPDATED, a.GEN_DATE, b.PLAYER_NAME, c.SEASON_NAME, d.NAME " +
                " from TENNIS_CHAMPION_PLAYER a join TENNIS_PLAYER b ON a.PLAYER_ID = b.ID join TENNIS_SEASON c on a.SEASON_ID = c.ID " +
                " join TENNIS_CHAMPION d on a.CHAMPION_CODE = d.CODE " +
                " where a.ID = ? ";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setBigDecimal(1, id);
            
            rs = preStmt.executeQuery();
            if (rs.next()) {
            	championPlayer = new TennisChampionPlayer();
            	championPlayer.setId(rs.getBigDecimal(1));
            	championPlayer.setChampionCode(rs.getString(2));
            	championPlayer.setSeasonId(rs.getBigDecimal(3));
            	championPlayer.setPlayerId(rs.getBigDecimal(4));
            	championPlayer.setPhaseId(rs.getBigDecimal(5));
            	championPlayer.setSeed(rs.getString(6));
            	championPlayer.setLastUpdated(rs.getTimestamp(7));
            	championPlayer.setGenDate(rs.getTimestamp(8));
            	championPlayer.setPlayerName(rs.getString(9));
            	championPlayer.setSeasonName(rs.getString(10));
            	championPlayer.setChampionName(rs.getString(11));
            }
        }
        catch (SQLException e) {
            logger.error("getRow: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt, rs);
        }
        return championPlayer;
    }
    
    /**
     * function find all Player in champion of season and phase.
     * @param championCode
     * @param seasonId
     * @param phaseId
     * @return Vector<TennisChampionPlayer> 
     */
    public Vector<TennisChampionPlayer> findAll(String championCode, BigDecimal seasonId, BigDecimal phaseId) {
    	Connection conn = null;
    	PreparedStatement preStmt = null;
    	ResultSet rs = null;
    	String strSQL = null;
    	TennisChampionPlayer championPlayer = null;
    	Vector <TennisChampionPlayer> vChampionPlayer = null;
    	
    	try {
    		conn = poolX.getConnection();
    		strSQL =
                " select a.ID, a.CHAMPION_CODE, SEASON_ID, PLAYER_ID, PHASE_ID, SEED, a.LAST_UPDATED, a.GEN_DATE, b.PLAYER_NAME, c.SEASON_NAME " +
                " from TENNIS_CHAMPION_PLAYER a join TENNIS_PLAYER b ON a.PLAYER_ID = b.ID join TENNIS_SEASON c on a.SEASON_ID = c.ID " +
                " where a.CHAMPION_CODE = ? and a.SEASON_ID = ? and a.PHASE_ID = ? ";
    		preStmt = conn.prepareStatement(strSQL);
    		preStmt.setString(1, championCode);
    		preStmt.setBigDecimal(2, seasonId);
    		preStmt.setBigDecimal(3, phaseId);
    		
    		rs = preStmt.executeQuery();
    		vChampionPlayer = new Vector<TennisChampionPlayer>();
    		while (rs.next()) {
    			championPlayer = new TennisChampionPlayer();
            	championPlayer.setId(rs.getBigDecimal(1));
            	championPlayer.setChampionCode(rs.getString(2));
            	championPlayer.setSeasonId(rs.getBigDecimal(3));
            	championPlayer.setPlayerId(rs.getBigDecimal(4));
            	championPlayer.setPhaseId(rs.getBigDecimal(5));
            	championPlayer.setSeed(rs.getString(6));
            	championPlayer.setLastUpdated(rs.getTimestamp(7));
            	championPlayer.setGenDate(rs.getTimestamp(8));
            	championPlayer.setPlayerName(rs.getString(9));
            	championPlayer.setSeasonName(rs.getString(10));
    			
    			vChampionPlayer.addElement(championPlayer);
    		}
    	}
    	catch (SQLException e) {
    		logger.error("findAll: Error executing SQL " + strSQL + ">>>" + e.toString());
    	} finally {
    		poolX.releaseConnection(conn, preStmt, rs);
    	}
    	return vChampionPlayer;
    }
	
    /**
     * function insert a TennisChampionPlayer
	 * @param championCode
	 * @param seasonId
	 * @param playerId
	 * @param phaseId
	 * @param seed
     * @return true if insert success else return false.
     */
	public boolean insertRow(String championCode, BigDecimal seasonId, BigDecimal playerId, BigDecimal phaseId, String seed){
		Connection conn = null;
        PreparedStatement preStmt = null;
        boolean result = false;
        String strSQL = null;
        try {
            conn = poolX.getConnection();

        	strSQL =" insert into TENNIS_CHAMPION_PLAYER( ID, CHAMPION_CODE, SEASON_ID, PLAYER_ID, PHASE_ID, SEED, LAST_UPDATED, GEN_DATE ) " +
    				" values(TENNIS_CHAMPION_PLAYER_SEQ.nextval, ?, ?, ?, ?, ?, sysdate, sysdate) ";
	        preStmt = conn.prepareStatement(strSQL);
	        preStmt.setString(1, championCode);
	        preStmt.setBigDecimal(2, seasonId);
	        preStmt.setBigDecimal(3, playerId);
	        preStmt.setBigDecimal(4, phaseId);
	        preStmt.setString(5, seed);
	        
	        if (preStmt.executeUpdate()>=1) {
	        	result = true;
	        }
        } catch (Exception ex) {
        	logger.error("insertRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
        } finally {
        	poolX.releaseConnection(conn, preStmt);
       }
       return result;
	}
	
	 /**
	  * function update a TennisChampionPlayer 
	  * @param id
	  * @param championCode
	  * @param seasonId
	  * @param playerId
	  * @param phaseId
	  * @param seed
	  * @return true if update success else return false.
	  */
	public boolean updateRow(BigDecimal id, String championCode, BigDecimal seasonId, BigDecimal playerId, BigDecimal phaseId, String seed){
		Connection conn = null;
        PreparedStatement preStmt = null;
        boolean result = false;
        String strSQL = null;
        try {
        	conn = poolX.getConnection();
        	strSQL =" update TENNIS_CHAMPION_PLAYER set CHAMPION_CODE = ?, SEASON_ID = ?, PLAYER_ID = ?, PHASE_ID = ?, SEED = ?, LAST_UPDATED = sysdate " +
        			" where ID = ?";
        	preStmt = conn.prepareStatement(strSQL);
        	preStmt.setString(1, championCode);
        	preStmt.setBigDecimal(2, seasonId);
	        preStmt.setBigDecimal(3, playerId);
	        preStmt.setBigDecimal(4, phaseId);
	        preStmt.setString(5, seed);
        	preStmt.setBigDecimal(6, id);
        	
        	if(preStmt.executeUpdate() > 0){
        		result = true;
        	}                
        } catch (Exception ex) {
        	logger.error("updateRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
        } finally {
        	poolX.releaseConnection(conn, preStmt);
       }
        return result;
	}	

	/**
	 * function delete a TennisChampionPlayer
	 * @param id
	 * @return true if update success else return false.
	 */
	public boolean deleteRow(BigDecimal id){
		Connection conn = null;
		PreparedStatement preStmt = null;
		boolean result = false;
		String strSQL = null;
		try {
			conn = poolX.getConnection();
			strSQL =" delete from TENNIS_CHAMPION_PLAYER where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			if(preStmt.executeUpdate() > 0){
				result = true;
			}
		} catch (Exception ex) {
			logger.error("deleteRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt);
		}
		return result;
	}
}
